package com.itcaius.myandroidhomework.data.entity;

import static com.itcaius.myandroidhomework.Context.UserTableContext.USER_TABLE_COLUMN_GENDER;
import static com.itcaius.myandroidhomework.Context.UserTableContext.USER_TABLE_COLUMN_ID;
import static com.itcaius.myandroidhomework.Context.UserTableContext.USER_TABLE_COLUMN_PASSWORD;
import static com.itcaius.myandroidhomework.Context.UserTableContext.USER_TABLE_COLUMN_USERNAME;
import static com.itcaius.myandroidhomework.Context.UserTableContext.USER_TABLE_NAME;

import androidx.room.ColumnInfo;
import androidx.room.Entity;
import androidx.room.Ignore;
import androidx.room.Index;
import androidx.room.PrimaryKey;
import androidx.room.TypeConverters;

import com.itcaius.myandroidhomework.utils.Gender;
import com.itcaius.myandroidhomework.utils.GenderConverter;

import org.mindrot.jbcrypt.BCrypt;


@Entity(
        tableName = USER_TABLE_NAME,
        indices = {
                @Index(value = {USER_TABLE_COLUMN_USERNAME}, unique = true)
        }
)
public class UserEntity {

    @PrimaryKey(autoGenerate = true)
    @ColumnInfo(name = USER_TABLE_COLUMN_ID)
    private Long id;

    @ColumnInfo(name= USER_TABLE_COLUMN_USERNAME)
    private String username;
    @ColumnInfo(name= USER_TABLE_COLUMN_PASSWORD)
    private String password;
    @ColumnInfo(name= USER_TABLE_COLUMN_GENDER)
    @TypeConverters(GenderConverter.class)
    private Gender gender;

    //无参构造函数
    @Ignore
    public UserEntity() {}

    //全参构造函数
    public UserEntity(String username, String password, Gender gender) {
        this.username = username;
        this.password = BCrypt.hashpw(password, BCrypt.gensalt());
        this.gender = gender;
    }

    //必须实现getter和setter方法
    public String getPassword() {
        return this.password;
    }

    /**
     * Sets a new password for the user, if it's different from the current password,
     * it hashes the new password before storing it.
     *
     * @param newPassword The new plaintext password.
     */
    public void setPassword(String newPassword) {
        if (!BCrypt.checkpw(newPassword, this.password)) {
            this.password = BCrypt.hashpw(password, BCrypt.gensalt());
        }
    }

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public Gender getGender() {
        return gender;
    }

    public void setGender(Gender gender) {
        this.gender = gender;
    }

    /**
     * Checks if the input password is correct.
     *
     * @param rawPassword user input password
     * @return true if the password is correct, otherwise false
     */
    public boolean checkPassword(String rawPassword){
        return BCrypt.checkpw(rawPassword,this.password);
    }


    @Override
    public String toString(){
        return "UserEntity{" +
                "id=" + id +
                ", username='" + username + '\'' +
                ", password='" + password + '\'' +
                ", gender=" + gender +
                '}';
    }
}
